package com.hdl.script.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hdl.script.model.CommonViewHolder


abstract class CommonRecyclerAdapter<T>(val context: Context) : RecyclerView.Adapter<CommonViewHolder>() {

    private val mData: MutableList<T> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        val itemView = LayoutInflater.from(context).inflate(viewType, parent, false)
        return CommonViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        convert(holder, mData[position], position)
    }

    abstract fun convert(holder: CommonViewHolder, item: T, position: Int)

    fun addObject(model: T) {
        mData.add(model)
    }

    fun clearData() {
        mData.clear()
    }

    fun addAll(elements: Collection<T>) {
        mData.addAll(elements)
    }

    override fun getItemCount(): Int {
        return mData.size
    }
}